#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
from optparse import OptionParser
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy import log, signals
from goods.spiders.zolSpider import ZolSpider
from scrapy.utils.project import get_project_settings
settings = get_project_settings()
sortList = settings['SORT_LIST']
def showHelp():
    print '--------------------------------------------------------------------'
    #print 'example: python start.py -a'
    #print 'example: python start.py -s 57'
    for k in sortList:
        print '\t%s\t%s\t%s'%(k,sortList[k][0],sortList[k][2].decode('utf-8'))
    print '--------------------------------------------------------------------'
    sys.exit()

def setup_crawler(k):
    if sortList.has_key(k):
        print 'start:%s-%s'%(sortList[k][0],sortList[k][2])
        spider = ZolSpider(k,sortList[k][0],sortList[k][1])
        settings = get_project_settings()
        crawler = Crawler(settings)
        crawler.configure()
        crawler.crawl(spider)
        crawler.start()
    else:
        print 'key:[%s] not exists'%k
        showHelp()

def start_callback(option, opt, value, parser):
    setup_crawler(value)
    log.start()
    reactor.run()

def startAll_callback(option, opt_str, value, parser):
    print 'start all spider'
    for k in sortList:
        setup_crawler(k)
    log.start()
    reactor.run()

def showlist_callback(option, opt_str, value, parser):
    showHelp()

def main():
    usage = "usage: %prog [options] args"
    parser = OptionParser(usage,version="%prog 0.1 ")
    parser.add_option("-l", action="callback", callback=showlist_callback,help='显示任务列表'.decode('utf-8'))
    parser.add_option("-s", action="callback",type="string",dest="index", callback=start_callback,help='启动单个任务'.decode('utf-8'))
    parser.add_option("-a", action="callback", callback=startAll_callback,help='启动所有任务'.decode('utf-8'))
    (options, args) = parser.parse_args()

if __name__ == '__main__':
    main()

